Skip to content

Conversation

VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Aug 30, 2025

Cf https://www.php.net/manual/en/language.types.array.php

Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

So ObjectType::toArrayKey should be an error.

@VincentLanglet
Copy link
Contributor Author

VincentLanglet commented Aug 30, 2025

@ondrejmirtes you reported in #4164

This looks wrong for array{foo: 17, bar: 19}|array{baz: 21}. int|object never exists on these arrays.

The reason was the fact that array[$object] was considered as "Maybe" rather than no,
as shown by https://phpstan.org/r/c6822927-88f1-4a23-a2c0-bb2fb2662e2a

I tried to fix this, but now I'm getting the a LegacyNodeScope test failure because

@var array[array]

is now understood as an error and not mixed ; it's related to

return new ErrorType();

Does the change seems ok to you ? If not any idea how I should fix it ?

@VincentLanglet
Copy link
Contributor Author

What I don't get currently is why

@param array[array]

is considered as mixed by PHPStan, and why

@var array[array]

is considered as *ERROR*.

Changing this might be a correct way to finish this PR ; I'm open to any advice ondrej.

@VincentLanglet
Copy link
Contributor Author

The behavior for array[array] is now consistent with
https://phpstan.org/r/712fa80d-be94-4efb-b5e7-c700c9c1a76e

So I had to update the test.

@VincentLanglet VincentLanglet marked this pull request as ready for review August 31, 2025 12:02
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@@ -1801,7 +1801,7 @@ public static function dataProperties(): array
'$this->resource',
],
[
'mixed',
'*ERROR*',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phpdoc use array[array]

@ondrejmirtes ondrejmirtes merged commit 547aff4 into phpstan:2.1.x Sep 2, 2025
450 of 457 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants